﻿using God.ShoppingWebApi.DTO;
using God.ShoppingWebApi.Entities;
using God.ShoppingWebApi.IService;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using static Volo.Abp.Identity.Settings.IdentitySettingNames;

namespace God.ShoppingWebApi.Service
{
    public class AdminUsersService : ApplicationService, IAdminUserService
    {
        /// <summary>
        /// 用户
        /// </summary>
           IRepository<UserLogin, Guid> _user;
        public AdminUsersService(IRepository<UserLogin, Guid> user)
        {
            _user = user;
        }
        [HttpPost]
        [Route("api/UserShow")]
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userDTO"></param>
        /// <returns></returns>
        public async Task AddAsync(UserDTO userDTO)
        {
            UserLogin ci = new UserLogin(
                GuidGenerator.Create(),

                           userDTO.UserName,
                          userDTO.UserSex,
                           userDTO.UserPhone,
                          userDTO.UserAge,
                          userDTO.UserEmail,
                           userDTO.UserInfoName,
                         userDTO.UserPassword,
                            userDTO.UserInfoRemarks,
                CurrentUser.Id);

            await _user.InsertAsync(ci);
        }
        [Route("api/DelUser")]
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="gu"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid gu)
        {
            await _user.DeleteAsync(gu);
        }
        [HttpGet]
        [Route("api/EditUser")]
        /// <summary>
        /// 反填用户
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>

        public async Task<UserDTO> Edit(Guid gid)
        {
            var data = await _user.GetAsync(gid);
            var UserDTO = ObjectMapper.Map<UserLogin, UserDTO>(data);
            return UserDTO;
        }
        [HttpGet]
        [Route("api/UserShow")]
        /// <summary>
        /// 显示用户
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetListAsync()
        {
            var str = await _user.GetListAsync();

            return ObjectMapper.Map<List<UserLogin>, List<UserDTO>>(str);
        }
        [Route("api/UserUpdate")]
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="userDTO"></param>
        /// <returns></returns>

        public  async Task<UserDTO> Update(UserDTO userDTO)
        {
            var data = await _user.GetAsync(userDTO.Id);
            if (data != null)
            {
                data.LastModificationTime = DateTime.Now;
                data.UserName = userDTO.UserName;
                data.UserSex = userDTO.UserSex;
                data.UserPhone = userDTO.UserPhone;
                data.UserAge = userDTO.UserAge;
                data.UserEmail = userDTO.UserEmail;
                data.UserInfoName = userDTO.UserInfoName;
                data.UserPassword = userDTO.UserPassword;
                data.UserInfoRemarks = userDTO.UserInfoRemarks;
                var Info = await _user.UpdateAsync(data);

                return userDTO;
            }
            else
            {
                return null;
            }
        }
    }
}
